local iputils = require("resty.iputils")
local redis = require("resty.redis")

local blocked_ips = {}
local red = redis:new()
red:set_timeout(2000) -- 2s

local ok ,err = red:connect("127.0.0.1",6379)
if not ok then
	return
end

local res, err = red:auth("qD-tq4xEx")
if not res then
	--ngx.say("failed to authenticate: ", err)
	return
end

res , err = red:SMEMBERS("block_ips")
if not res then
	return
end

for i, v in ipairs(res) do  
	table.insert(blocked_ips,v)
end  


local headers = ngx.req.get_headers()
local client_ip = headers["X-FORWARDED-FOR"] or headers["X-REAL-IP"]

blocklist = iputils.parse_cidrs(blocked_ips)
if iputils.ip_in_cidrs(client_ip , blocklist) then
	ngx.status = ngx.OK
	local resp="{\"code\":200,\"result\":true,\"message\":\"\",\"data\":{\"url\":\"/403.html\"},\"md5\":\"9f587448e3752cb59df72f6c98e06d41\",\"city\":\"gz\"}"
	return ngx.say(resp)
end


local ip_time_out=60
local ip_max_count=5

ip_count , err = red:get("upload-" .. client_ip )
if ip_count == ngx.null then
	res , err = red:setex("upload-" .. client_ip ,ip_time_out , 1)
else
	ip_count = ip_count + 1
	ttl , err = red:ttl("upload-" .. client_ip)
	res , err = red:setex("upload-" .. client_ip , ttl, ip_count)
		
	if ip_count >= ip_max_count then
		ngx.status = ngx.OK
		local resp="{\"code\":200,\"result\":true,\"message\":\"\",\"data\":{\"url\":\"/509.html\"},\"md5\":\"9f587448e3752cb59df72f6c98e06d41\",\"city\":\"gz\"}"
        	return ngx.say(resp)
    	end
end

local ip_time_out2 = 5
local ip_max_count2 = 2

ip_count2 , err = red:get("upload2-" .. client_ip )
if ip_count2 == ngx.null then
	res , err = red:setex("upload2-" .. client_ip ,ip_time_out2 , 1)
else
	ip_count2 = ip_count2 + 1
	ttl , err = red:ttl("upload2-" .. client_ip)
	res , err = red:setex("upload2-" .. client_ip , ttl, ip_count2)
		
	if ip_count2 >= ip_max_count2 then
		ngx.status = ngx.OK
		local resp="{\"code\":200,\"result\":true,\"message\":\"\",\"data\":{\"url\":\"/509.html\"},\"md5\":\"9f587448e3752cb59df72f6c98e06d41\",\"city\":\"gz\"}"
        	return ngx.say(resp)
    	end
end

local ok, err = red:set_keepalive(10000, 100)
if not ok then
	--ngx.say("failed to set keepalive: ", err)
	return
end
